package cn.pxy.ysframework.web.Controller;

import cn.pxy.ysframework.utils.Message;
import cn.pxy.ysframework.web.Dto.CreateCommentDto;
import cn.pxy.ysframework.web.Entity.Comment;
import cn.pxy.ysframework.web.Service.IAccountService;
import cn.pxy.ysframework.web.Service.ICardService;
import cn.pxy.ysframework.web.Service.ICommentService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;

@RestController
@RequestMapping(value = "/Comment")
@Api(description = "评论控制器--完成")
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private ICardService cardService;

    /**
     * 发帖
     * @param commentDto
     * @return
     */
    @PostMapping(value = "/CreateComment")
    @ApiOperation(value = "发帖--完成")
    public Message CreateComment(CreateCommentDto commentDto){
        Comment comment = new Comment();
        comment.setId(commentDto.getId());
        comment.setAccount(accountService.findById(commentDto.getAccount_Id()));
        comment.setCard(cardService.findById(commentDto.getCard_Id()));
        comment.setCommenttime(new Date(System.currentTimeMillis()));
        comment.setAccountname(commentDto.getAccountname());
        comment.setContent(commentDto.getContent());
        return new Message(JSON.toJSONString(commentService.save(comment)));
    }

    /**
     * 根据帖子id返回所有评论
     * @param cardId 帖子Id
     * @return
     */
    @PostMapping(value = "/FindCommentByCardId")
    @ApiOperation(value = "根据帖子id返回所有评论--完成")
    public Message FindCommentByCardId(String cardId){
        return new Message(JSON.toJSONString(commentService.findAllByCard(cardId)));
    }
}
